
package com.ce.cases.report.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import net.sf.json.JSONObject;

import com.ce.cases.report.dao.po.ReportTaxIllegal;
import com.ce.cases.report.logic.IReportIllegalLogic;
import com.ce.cases.source.dao.po.CaseSourceDrawApprove;
import com.ce.cases.source.dao.po.DjNsrxx;
import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.datamaintain.dao.po.AbstractPO;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.sys.org.dao.po.SysVOrg;
import com.ce.util.DbUtil;
import com.ce.util.FlowWorkUtil;
import com.ce.util.OrgUtil;

/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2014-03-12
* 创建时间：14:08:23
* 功能描述：检举信息台账 [REPORT_TAX_ILLEGAL]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class ReportTaxIllegalAction extends DataMaintainAction {
	private static final long serialVersionUID = 1L;
	
	private CaseSourceDrawApprove drawAppr;
	
	private IReportIllegalLogic reportIllegalLogic;
	
	@Override
	protected void initOtherData(boolean isNew) {
		if (isNew) {
			try {
				Date curDate = new Date();
				BeanAccessUtil.copyProperty(po, "recordDate", curDate);
				BeanAccessUtil.copyProperty(po, "recorder", User.getUserName());
				BeanAccessUtil.copyProperty(po, "reportTime", curDate);
				BeanAccessUtil.copyProperty(po, "dealResult", "1");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		User user=User.getUser();
		String rootOrgLevel=user.getRootOrgLevel();
		this.setAttribute("rootOrgLevel", rootOrgLevel);
		if("2".equals(rootOrgLevel)){
			ReportTaxIllegal poo=(ReportTaxIllegal)po;
			poo.setInspectionDeptId(user.getRootOrgId());
			poo.setInspectionDept(OrgUtil.getOrgName(poo.getInspectionDeptId()));
		} else if("3".equals(rootOrgLevel)){
			ReportTaxIllegal poo=(ReportTaxIllegal)po;			
			poo.setCollectionDeptId(user.getRootOrgId());
			poo.setCollectionDept(OrgUtil.getOrgName(poo.getCollectionDeptId()));
			
		} 
		String rootOrgId=user.getRootOrgId();
		this.setAttribute("rootOrgId", rootOrgId);
		
	}
	
	

	@Override
	protected String look() throws AppException {
		User user=User.getUser();
		String curUserId=user.getVcName();
		String selectId = this.getSelectId();
		ReportTaxIllegal po_=(ReportTaxIllegal) this.baselogic.getPOByPk(ReportTaxIllegal.class.getName(), selectId);
		String registerPerson=po_.getRegisterPerson();
		if(registerPerson.equals(curUserId)){
			this.setAttribute("readonly", "true");
		}
		return super.look();
	}



	@Override
	protected String addItem() throws AppException {
		//流程状态及登记人信息
		String dealResult=this.getParameter("po.dealResult");
		try {
			Date curDate = new Date();
			BeanAccessUtil.copyProperty(po, "registerDate", curDate);
			BeanAccessUtil.copyProperty(po, "registerPerson", User.getUserName());
			BeanAccessUtil.copyProperty(po, "registerDept", User.getUserOrgName());
			BeanAccessUtil.copyProperty(po, "registerDeptId", User.getUserOrgId());
			if("1".equals(dealResult)){
				BeanAccessUtil.copyProperty(po, "state", "3");
			}
			BeanAccessUtil.copyProperty(po, "stateInfo", "");
			ReportTaxIllegal poo=(ReportTaxIllegal)po;
			BeanAccessUtil.copyProperty(po, "collectionDept", OrgUtil.getOrgName(poo.getCollectionDeptId()));
			SysVOrg org=OrgUtil.getParOrg(poo.getCollectionDeptId());
			BeanAccessUtil.copyProperty(po, "inspectionDeptId", org.getVcId());
			BeanAccessUtil.copyProperty(po, "inspectionDept", org.getVcName());
			BeanAccessUtil.copyProperty(po, "directorPassFlag", "0");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return super.addItem();
	}

	@Override
	protected String modifyItem() throws AppException {
		//避免将流程状态暴漏给页面，避免非法篡改
		String dealResult=this.getParameter("po.dealResult");
		ReportTaxIllegal savePo=(ReportTaxIllegal)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
		ReportTaxIllegal poo=(ReportTaxIllegal)po;
		
		try {
			BeanAccessUtil.copyProperty(po, "collectionDept", OrgUtil.getOrgName(poo.getCollectionDeptId()));
			SysVOrg org=OrgUtil.getParOrg(poo.getCollectionDeptId());
			BeanAccessUtil.copyProperty(po, "inspectionDeptId", org.getVcId());
			BeanAccessUtil.copyProperty(po, "inspectionDept", org.getVcName());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		BeanAccessUtil.copyNoCopyNullProperty(po, savePo);
		if("1".equals(dealResult)){
			savePo.setState(3);
		}
		this.baselogic.updatePo(savePo);
		FlowWorkUtil.saveWorkflowMan(savePo.getVcId(),"修改");
	    return this.searchData();			
	}
	
	/**
	 * 处理情况登记页面
	 */
	public String dealPage(){
		String vcId=this.getSelectId();
		try {
			po=(AbstractPO)this.baselogic.getPOByPk(po.getClass().getName(), vcId);
			ReportTaxIllegal savePo=(ReportTaxIllegal)po;
			savePo.setDirectorApprDate(new Date());
		} catch (AppException e) {
			e.printStackTrace();
		}
		return "openDealResultPage";
	}
	/**
	 * 处理情况登记
	 */
	public String dealRegist(){
		try {
			ReportTaxIllegal savePo=(ReportTaxIllegal)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
			BeanAccessUtil.copyNoCopyNullProperty(po, savePo);
			ReportTaxIllegal pot=(ReportTaxIllegal)po;
			savePo.setDirectorPassFlag(pot.getDirectorPassFlag());
			savePo.setDirectorAdvice(pot.getDirectorAdvice());
			savePo.setDirectorApprDate(pot.getDirectorApprDate());			
			this.baselogic.updatePo(savePo);
		} catch (AppException e) {
			e.printStackTrace();
			return this.returnInfoForJS(false, e.getMessage());
		}
		return this.returnInfoForJS(true, "处理情况登记成功！");
	}
	/**
	 * 向征管局公开
	 */
	public String publicReportTaxIllegal(){
		 String[] vcIds = this.getParameterValues(WebConstants.SELECT_ITEMS_NAMES);
		 String retStr="";
		 if(vcIds!=null){
			   retStr=reportIllegalLogic.updatePublicSign(vcIds);
			   for(String vcId:vcIds){
				   FlowWorkUtil.saveWorkflowMan(vcId,"向征管局公开");
			   }
		   }
		   if(StringUtil.isBlank(retStr)){
			   return this.returnInfoForJS(true, "该信息推送征管局成功！"); 
		   }else{
			   return this.returnInfoForJS(false, retStr);  
		   }  
	}
	/**
	 * 提请审批页面
	 */
	public String drawPage(){
		String vcId=this.getSelectId();
		try {
			ReportTaxIllegal reportPo=(ReportTaxIllegal)this.baselogic.getPOByPk(po.getClass().getName(), vcId);
			drawAppr=new CaseSourceDrawApprove();
			drawAppr.setCaseFromNum("8");
			drawAppr.setCaseFromName("检举");
			drawAppr.setTaxpayerNumber(reportPo.getTaxpayerNumber());
			drawAppr.setDrawDept(User.getUserOrgName());
			drawAppr.setDrawDeptId(User.getUserOrgId());
			drawAppr.setInspectionDept(reportPo.getInspectionDept());
			drawAppr.setInspectionDeptId(reportPo.getInspectionDeptId());
			drawAppr.setCollectionDept(reportPo.getCollectionDept());
			drawAppr.setCollectionDeptId(reportPo.getCollectionDeptId());
		} catch (AppException e) {
			e.printStackTrace();
		}
		this.setAttribute("reportId", vcId);
		return "openDrawPage";
	}
	/**
	 * 提请审批保存
	 */
	public String dealDraw(){
		String reportId=this.getParameter("reportId");
		 String retStr=reportIllegalLogic.dealDraw(reportId, drawAppr);
		if(StringUtil.isBlank(retStr)){
			   return this.returnInfoForJS(true, "提请操作成功！"); 
		   }else{
			   return this.returnInfoForJS(false, retStr);  
		   }  
	}
	
	
	/**
	 * 处理签收
	 */
	public String dealSign(){
		   String[] vcIds = this.getParameterValues(WebConstants.SELECT_ITEMS_NAMES);
		   String retStr="";
		   if(vcIds!=null){
			   retStr=reportIllegalLogic.dealSign(vcIds);
			   for(String vcId:vcIds){
				   FlowWorkUtil.saveWorkflowMan(vcId,"签收");
			   }
		   }
		   if(StringUtil.isBlank(retStr)){
			   return this.returnInfoForJS(true, "签收成功！"); 
		   }else{
			   return this.returnInfoForJS(false, retStr);  
		   }  
	}
	
	/**
	 * 回复登记页面
	 */
	public String replyPage(){
		String vcId=this.getSelectId();
		try {
			po=(AbstractPO)this.baselogic.getPOByPk(po.getClass().getName(), vcId);
			ReportTaxIllegal reportTaxIllegal=(ReportTaxIllegal)po;
			if(reportTaxIllegal.getReplyDate()==null){
				BeanAccessUtil.copyProperty(po, "replyDate", new Date());
			}
			if(StringUtil.isBlank(reportTaxIllegal.getReplyPerson())){
				BeanAccessUtil.copyProperty(po, "replyPerson",User.getUserName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openReplyPage";
	}
	/**
	 * 回复情况登记
	 */
	public String dealReply(){
		try {
			ReportTaxIllegal savePo=(ReportTaxIllegal)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
			BeanAccessUtil.copyNoCopyNullProperty(po, savePo);
			this.baselogic.updatePo(savePo);
		} catch (AppException e) {
			e.printStackTrace();
			return this.returnInfoForJS(false, e.getMessage());
		}
		return this.returnInfoForJS(true, "回复情况登记成功！");
	}
	
	/**
	 * 回复登记页面
	 */
	public String rewardPage(){
		String vcId=this.getSelectId();
		try {
			po=(AbstractPO)this.baselogic.getPOByPk(po.getClass().getName(), vcId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "openRewardPage";
	}
	/**
	 * 奖励情况登记
	 */
	public String dealReward(){
		try {
			ReportTaxIllegal savePo=(ReportTaxIllegal)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
			BeanAccessUtil.copyNoCopyNullProperty(po, savePo);
			savePo.setRewardOperator(User.getUserName());
			savePo.setRewardDate(new Date());
			this.baselogic.updatePo(savePo);
		} catch (AppException e) {
			e.printStackTrace();
			return this.returnInfoForJS(false, e.getMessage());
		}
		return this.returnInfoForJS(true, "奖励情况登记成功！");
	}
	public String getNsrinfoByTaxpayerNumber(){
    	String taxpayerNumber=this.getParameter("taxpayerNumber");
    	DjNsrxx po=reportIllegalLogic.getDjNsrxxRemote(taxpayerNumber);
    	//System.out.println(JSONObject.fromObject(po).toString());
    	int cnt=DbUtil.getFirstIntSqlQuery("select count(*) from REPORT_TAX_ILLEGAL t where t.TAXPAYER_NUMBER='"+taxpayerNumber+"' and t.vc_del$flag='0'");
    	if(po!=null)
    		po.setStatus(cnt);//被检举次数
    	
    	if(null!=po)
    		return writeHTML(JSONObject.fromObject(po).toString());
    	else
    		return this.returnInfoForJS(false, "对不起，没有获取到纳税人信息，请确认输入的纳税人编号是否正确,或者查询核心征管系统确认该纳税人编号是否存在.");
    }
	
	public CaseSourceDrawApprove getDrawAppr() {
		return drawAppr;
	}

	public void setDrawAppr(CaseSourceDrawApprove drawAppr) {
		this.drawAppr = drawAppr;
	}

	public IReportIllegalLogic getReportIllegalLogic() {
		return reportIllegalLogic;
	}

	public void setReportIllegalLogic(IReportIllegalLogic reportIllegalLogic) {
		this.reportIllegalLogic = reportIllegalLogic;
	}

	

}
